package com.hm.hall.commons.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wei
 */
public class Covertor {
    public static final SimpleDateFormat dateFomart = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 字符串"yyyy-MM-dd"转换成日期
     *
     * @param str
     * @return date
     */
    public static Date strToDate(String str) {

        Date date = null;
        try {
            date = dateFomart.parse(str);
        } catch (ParseException e) {

        }
        return date;
    }

    /**
     * 日期转换成字符串  "yyyy-MM-dd"
     *
     * @param date
     * @return str
     */
    public static String dateToStr(Date date) {
        if (date == null) {
            return "";
        }
        return dateFomart.format(date);
    }

    /**
     * list 转英文逗号,连接的字符串
     *
     * @param <T>
     * @param list
     * @return
     */
    public static <T> String list2string(List<T> list) {
        if (list == null || list.size() == 0) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                sb.append(list.get(i));
                if (i != list.size() - 1) {
                    sb.append(",");
                }
            }
            return sb.toString();
        }
    }


    public static String delHTMLTag(String htmlStr) {
        String regExScript = "<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String regExStyle = "<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String regExHtml = "<[^>]+>"; //定义HTML标签的正则表达式

        Pattern pScript = Pattern.compile(regExScript, Pattern.CASE_INSENSITIVE);
        Matcher mScript = pScript.matcher(htmlStr);
        htmlStr = mScript.replaceAll(""); //过滤script标签

        Pattern pStyle = Pattern.compile(regExStyle, Pattern.CASE_INSENSITIVE);
        Matcher mStyle = pStyle.matcher(htmlStr);
        htmlStr = mStyle.replaceAll(""); //过滤style标签

        Pattern pHtml = Pattern.compile(regExHtml, Pattern.CASE_INSENSITIVE);
        Matcher mHtml = pHtml.matcher(htmlStr);
        htmlStr = mHtml.replaceAll(""); //过滤html标签

        return htmlStr.trim(); //返回文本字符串
    }
}

